﻿Imports System.ComponentModel

Namespace Controls
    Public Class ProgressRing
        Inherits Control

        Dim hasAppliedTemplate As Boolean = False

        Public Sub New()
            DefaultStyleKey = GetType(ProgressRing)
            TemplateSettings = New TemplateSettingValues(60)
        End Sub

        Public Overrides Sub OnApplyTemplate()
            MyBase.OnApplyTemplate()
            hasAppliedTemplate = True
            UpdateState(IsActive)
        End Sub

        Sub UpdateState(isActive As Boolean)
            If hasAppliedTemplate Then
                Dim state = If(isActive, "Active", "Inactive")
                VisualStateManager.GoToState(Me, state, True)
            End If
        End Sub

        Protected Overrides Function MeasureOverride(availableSize As Size) As Size
            Dim width = 100.0
            If Not DesignerProperties.IsInDesignTool Then
                width = If(Me.Width <> Double.NaN, Me.Width, availableSize.Width)
            End If
            TemplateSettings = New TemplateSettingValues(width)
            Return MyBase.MeasureOverride(availableSize)
        End Function

        Public Property IsActive As Boolean
            Get
                Return GetValue(IsActiveProperty)
            End Get
            Set(value As Boolean)
                SetValue(IsActiveProperty, value)
            End Set
        End Property
        Public Shared ReadOnly IsActiveProperty As DependencyProperty = DependencyProperty.Register("IsActive", GetType(Boolean), GetType(ProgressRing), New PropertyMetadata(False, New PropertyChangedCallback(AddressOf IsActiveChanged)))
        Private Shared Sub IsActiveChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
            Dim pr = DirectCast(d, ProgressRing)
            Dim isActive = CBool(e.NewValue)
            pr.UpdateState(isActive)
        End Sub

        Public Property TemplateSettings As TemplateSettingValues
            Get
                Return GetValue(TemplateSettingsProperty)
            End Get
            Set(value As TemplateSettingValues)
                SetValue(TemplateSettingsProperty, value)
            End Set
        End Property
        Public Shared ReadOnly TemplateSettingsProperty As DependencyProperty = DependencyProperty.Register("TemplateSettings", GetType(TemplateSettingValues), GetType(ProgressRing), New PropertyMetadata(New TemplateSettingValues(100)))
    End Class

    Public Class TemplateSettingValues
        Inherits DependencyObject

        Public Sub New(width As Double)
            MaxSideLength = 400
            EllipseDiameter = width / 10
            EllipseOffset = New Thickness(EllipseDiameter)
        End Sub

        Public Property MaxSideLength() As Double
            Get
                Return GetValue(MaxSideLengthProperty)
            End Get
            Set
                SetValue(MaxSideLengthProperty, Value)
            End Set
        End Property
        Public Shared ReadOnly MaxSideLengthProperty As DependencyProperty = DependencyProperty.Register("MaxSideLength", GetType(Double), GetType(TemplateSettingValues), New PropertyMetadata(0.0))

        Public Property EllipseDiameter() As Double
            Get
                Return GetValue(EllipseDiameterProperty)
            End Get
            Set
                SetValue(EllipseDiameterProperty, Value)
            End Set
        End Property
        Public Shared ReadOnly EllipseDiameterProperty As DependencyProperty = DependencyProperty.Register("EllipseDiameter", GetType(Double), GetType(TemplateSettingValues), New PropertyMetadata(0.0))

        Public Property EllipseOffset() As Thickness
            Get
                Return GetValue(EllipseOffsetProperty)
            End Get
            Set
                SetValue(EllipseOffsetProperty, Value)
            End Set
        End Property
        Public Shared ReadOnly EllipseOffsetProperty As DependencyProperty = DependencyProperty.Register("EllipseOffset", GetType(Thickness), GetType(TemplateSettingValues), New PropertyMetadata(New Thickness))
    End Class
End Namespace